# -*- coding: utf-8 -*-

import sys
import time
import win32api
import win32con
from utils.log_util import loggings as logger
import frozen_dir as fdir
from utils.configutil import ConfigUtil
import pathlib


def check_regtime(valid_date):
    try:
        v_date = valid_date
        str1 = time.strftime("%Y%m%d%H%M%S", time.localtime())
        ctime = int(str1)
        if ctime > v_date or ctime < 20181221105959:
            return False
        else:
            key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,
                                      'SOFTWARE\\Microsoft', 0, win32con.KEY_ALL_ACCESS)
            rk1 = win32api.RegCreateKey(key, 'HXKJ')
            tt = win32api.RegQueryValueEx(rk1, 'CTIME')
            otime = int(tt[0])
            if otime > ctime:
                win32api.RegCloseKey(key)
                return False
            else:
                win32api.RegSetValueEx(rk1, 'CTIME', 0, win32con.REG_SZ, str1)
                tt2 = win32api.RegQueryValueEx(rk1, 'NUMBER')
                f2 = int(tt2[0]) + 1
                str1 = '%s' % f2
                win32api.RegSetValueEx(rk1, 'NUMBER', 0, win32con.REG_SZ, str1)
                win32api.RegCloseKey(key)
                return True
    except Exception as e:
        win32api.RegSetValueEx(rk1, "CTIME", 0, win32con.REG_SZ, '20220812115959')
        win32api.RegSetValueEx(rk1, "NUMBER", 0, win32con.REG_SZ, '0')
        logger.error("Check_Regtime,Something wrong happened...," + str(e))
        return False


def get_software_reference():
    try:
        initdir = fdir.app_path()
        config_path = str(initdir) + "\static\gui_setting.ini"
        configutil = ConfigUtil(config_path)
        reference = configutil.getstring_value_with_section_options("software", "reference")
        return reference
    except Exception as e:
        logger.error("get_software_reference," + str(e))


if __name__ == "__main__":
    if check_regtime(20221116144059):
        print('time valid')
    else:
        print('time invalid')

    # print(get_software_reference())
